<template>
  <mivaLink :style="wrapperStyle" :to="spacePath" class="global-avatar__module">
    <el-avatar :size="size" :src="baseAvatar" class="avatar__item" draggable="false" fit="cover" icon="el-icon-user-solid" />
    <template v-if="pendantShow">
      <img :src="pendant" :style="pendantStyle" class="pendant__item" draggable="false" />
    </template>
  </mivaLink>
</template>

<script>
import defaultPendant from '@/assets/defaultPendant5.png';

export default {
    name: 'MivaAvatar',
    props: {
        userInfo: {
            type: Object,
            default: () => ({}),
        },
        uid: {
            type: Number,
            default: 0,
        },
        avatar: {
            type: String,
            default: '',
        },
        showPendant: {
            type: Boolean,
            default: true,
        },
        pendant: {
            type: String,
            default: defaultPendant,
        },
        size: {
            type: Number,
            default: 48,
        },
    },
    computed: {
        baseUid() {
            return this.uid || this.userInfo.uid;
        },
        baseAvatar() {
            return this.avatar || this.userInfo.avatar;
        },
        basePendant() {
            return this.userInfo.pendant || this.pendant;
        },
        spacePath() {
            return `/space/${this.baseUid}`;
        },
        wrapperStyle() {
            return { width: `${this.size}px`, height: `${this.size}px` };
        },
        pendantStyle() {
            return { width: `${this.size * 2}px`, height: `${this.size * 2}px` };
        },
        pendantShow() {
            return this.basePendant && this.showPendant;
        },
    },
};
</script>

<style lang="less">
.global-avatar__module {
  position: relative;
  display: inline-block;
  user-select: none;
  flex-shrink: 0;
  .avatar__item,
  .pendant__item {
    max-width: initial;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;
    object-fit: cover;
  }
}
</style>
